package com.example.springboot;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

/**
 * @Author https://www.javastudy.cloud
 * @CreateTime 2019/11/3
 **/
@RestController
public class ParamController {

    /**
     * 这里使用了 GetMapping 和 @RequestMapping(method = RequestMethod.GET)是一样的
     * 在方法入参的位置,把每个参数都平铺开来
     *
     * @return
     */
    @GetMapping("param/handleGetParam1")
    public String handleGetParam1(String param1, String param2) {

        if ("辛夷坞".equals(param1)) {
            AuthorInfo authorInfo = new AuthorInfo();
            ObjectMapper objectMapper = new ObjectMapper();
            String userJsonStr = "";
            try {
                userJsonStr = objectMapper.writeValueAsString(authorInfo);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            return userJsonStr;
        } else {
            String result = param1 + "工作认真的样子也太酷了把，简称酷男";
//        String result = String.format("添少工作认真的样子也太酷了把，简称酷男  in handle1 param1 is %s, param2 is %s", param1, param2);
            return result;
        }


    }

    /**
     * 把入参封装成一个实体,如果请求参数过多,一般大于5个时,用这种方法
     *
     * @param demo
     * @return
     */
    @GetMapping("param/handleGetParam2")
    public String handleGetParam2(ParamDemo demo) {
        String result = String.format("in handle2 param1 is %s, param2 is %s", demo.param1, demo.param2);
        return result;
    }

    class AuthorInfo {
        String fullName = "辛夷坞，原名蒋春玲";//作者
        String birth = "1981年8月4日";//出生日期
        String birthplace = "广西桂林";//出生地
        String graduationSchool = "广西师范学院";//毕业学院
        String nationality = "中国";//国籍
        String occupation = "作家";//职业
        String nation = "汉族";//民族
        String economicCompany = "北京白马时光文化发展有限公司";//经济公司
        String representativeWorks = "原来你还在这里、致我们终将逝去的青春、我们、针尖蜜";//代表作品

        public String getFullName() {
            return fullName;
        }

        public void setFullName(String fullName) {
            this.fullName = fullName;
        }

        public String getBirth() {
            return birth;
        }

        public void setBirth(String birth) {
            this.birth = birth;
        }

        public String getBirthplace() {
            return birthplace;
        }

        public void setBirthplace(String birthplace) {
            this.birthplace = birthplace;
        }

        public String getGraduationSchool() {
            return graduationSchool;
        }

        public void setGraduationSchool(String graduationSchool) {
            this.graduationSchool = graduationSchool;
        }

        public String getNationality() {
            return nationality;
        }

        public void setNationality(String nationality) {
            this.nationality = nationality;
        }

        public String getOccupation() {
            return occupation;
        }

        public void setOccupation(String occupation) {
            this.occupation = occupation;
        }

        public String getNation() {
            return nation;
        }

        public void setNation(String nation) {
            this.nation = nation;
        }

        public String getEconomicCompany() {
            return economicCompany;
        }

        public void setEconomicCompany(String economicCompany) {
            this.economicCompany = economicCompany;
        }

        public String getRepresentativeWorks() {
            return representativeWorks;
        }

        public void setRepresentativeWorks(String representativeWorks) {
            this.representativeWorks = representativeWorks;
        }
    }


    /**
     * 使用静态内部类,简单定义一个入参实体类
     */
    static class ParamDemo {
        private String param1;
        private String param2;

        public String getParam1() {
            return param1;
        }

        public void setParam1(String param1) {
            this.param1 = param1;
        }

        public String getParam2() {
            return param2;
        }

        public void setParam2(String param2) {
            this.param2 = param2;
        }
    }


    /**
     * Post 请求入参也可以这样平铺使用
     * 一般适用于前端Header中Content-Type为application/x-www-form-urlencoded的场景
     *
     * @param param1
     * @param param2
     * @return
     */
    @PostMapping("param/handlePostParam1")
    public String handlePostParam1(String param1, String param2) {
        String result = String.format("in handle1 param1 is %s, param2 is %s", param1, param2);
        return result;
    }

    /**
     * 入参为一个实体类的形式,没有任何注解
     * 一般适用于参数个数较多,在Header中Content-Type为application/x-www-form-urlencoded的场景    * 一般适用于参数个数较多,在Header中Content-Type为application/x-www-form-urlencoded的场景
     *
     * @param demo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "param/handlePostParam2", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String handlePostParam2(@RequestBody ParamDemo demo) {
        if ("辛夷坞".equals(demo.param1)) {
            AuthorInfo authorInfo = new AuthorInfo();
            ObjectMapper objectMapper = new ObjectMapper();
            String userJsonStr = "";
            try {
                userJsonStr = objectMapper.writeValueAsString(authorInfo);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return userJsonStr;
        } else {
            String result = String.format("添少工作认真的太酷了，简称酷男  in handle2 param1 is %s, param2 is %s", demo.param1, demo.param2);
            return result;
        }
    }

    /**
     * 请求入参是一个实体,并且加上了 @RequestBody
     * 一般适用于前端Header中Content-Type 为 application/json的场景
     * 注意入参要是json格式
     *
     * @param demo
     * @return
     */
    @PostMapping("param/handlePostParam3")
    public String handlePostParam3(@RequestBody ParamDemo demo) {
        String result = String.format("in handle2 param1 is %s, param2 is %s", demo.param1, demo.param2);
        return result;
    }


}
